<?php
namespace app\api\controller;

use app\facade\Basic;
use think\Request;
use app\api\service\Address as ServiceAddress;

class Address extends BaseController
{


    /**
     *  地址列表
    */
    public function list(Request $request)
    {
        $param['user_id'] = $request->user_id;
        $param['listRows'] = $request->param('listRows',15);
        $param['type'] = $request->param('type',1);
        $this->validateData($param,'Address.list');
        $ServiceAddress = new ServiceAddress();
        $result = $ServiceAddress->list($param);

        return Basic::json($result);
    }

    /**
     *  地址列表
    */
    public function getList(Request $request) {

        $param['user_id'] = $request->user_id;

        $param['type'] = $request->param('type',1);

        $this->validateData($param,'Address.getList');
        $ServiceAddress = new ServiceAddress();
        $result = $ServiceAddress->getList($param);
        return Basic::json($result);
    }

    /**
     *  地址详情
    */
    public function detail(Request $request)
    {

        $param['user_id'] = $request->user_id;
        $param['id'] = $request->param('id',null);

        /* 验证 */
        $this->validateData($param,'Address.detail');

        $serviceRetrieve = new ServiceAddress();
        $result = $serviceRetrieve->detail($param);

        return Basic::json($result);

    }

    /**
     *  添加 & 修改地址
     */
    public function save(Request $request)
    {
        $param['user_id'] = $request->user_id;
        $param['id'] = $request->param('id',null);
        $param['type'] = $request->param('type',1);
        $param['first_name'] = $request->param('first_name',null);
        $param['last_name'] = $request->param('last_name',null);
        $param['email'] = $request->param('email',null);
        $param['phone'] = $request->param('phone',null);
        $param['country_id'] = $request->param('country_id',null);
        $param['city_id'] = $request->param('city_id',null);
        $param['city_name'] = $request->param('city_name',null);
        $param['zip_code'] = $request->param('zip_code',null);
        $param['address'] = $request->param('address',null);
        $param['remark'] = $request->param('remark',null);
        $param['company'] = $request->param('company',null);
        $param['address2'] = $request->param('address2',null);
        $param['is_default'] = $request->param('is_default',0);

        /* 验证 */
        $this->validateData($param,'Address.save');

        $serviceRetrieve = new ServiceAddress();
        $result = $serviceRetrieve->save($param);

        return Basic::json($result);
    }

    /**
     *  设置默认地址
     */
    public function setDefault(Request $request)
    {
        $param['user_id'] = $request->user_id;
        $param['id'] = $request->param('id',null);
        $param['type'] = $request->param('type',null);

        /* 验证 */
        $this->validateData($param,'Address.setDefault');


        $serviceRetrieve = new ServiceAddress();
        $result = $serviceRetrieve->setDefault($param);

        return Basic::json($result);
    }


    /**
     *  删除地址
    */
    public function del(Request $request)
    {
        $param['user_id'] = $request->user_id;
        $param['id'] = $request->param('id',null);

        /* 验证 */
        $this->validateData($param,'Address.del');

        $serviceRetrieve = new ServiceAddress();
        $result = $serviceRetrieve->del($param);

        return Basic::json($result);
    }





}
